package com.example.fourm.controller;

import com.example.fourm.common.AppResult;
import com.example.fourm.common.ResultCode;
import com.example.fourm.config.AppConfig;
import com.example.fourm.exception.ApplicationException;
import com.example.fourm.model.Article;
import com.example.fourm.model.Board;
import com.example.fourm.model.User;
import com.example.fourm.services.IArticleService;
import com.example.fourm.services.IBoardService;
import com.example.fourm.services.IUserService;
import com.example.fourm.services.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IUserService userService;
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam("title") @NonNull String title,
                            @RequestParam("content")@NonNull String content,
                            @RequestParam("boardId")@NonNull long boardId) {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        Board board = boardService.selectByPrimaryKey(boardId);
        if(board == null){
            log.warn("板块不存在");
            throw new ApplicationException(AppResult.failure(ResultCode.FAILED_CREATE));
        }
        Article article = new Article();
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        article.setUserId(user.getId());
        articleService.create(article);
        return AppResult.success("新增文章成功！！");
    }

    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@RequestParam(value = "boardId",required = false) Long boardId) {

        List<Article> articles = null;
        if(boardId == null){
            articles = articleService.selectAll();

        }else{
            articles = articleService.selectAllByBoardId(boardId);
        }
        if(articles == null){
            //如果为null那么序列化会结果为四个字母，转换为顺序表会序列化为【】
            log.info("article为空");
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);
    }

    @GetMapping("/details")
    public AppResult<Article> getDatils(@RequestParam("id") @NonNull Long id,
                                        HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);

        Article article = articleService.selectDetailById(id);
        if(article == null){
           return AppResult.failure(ResultCode.FAILED_NOT_EXISTS);
        }
        if(user.getId() == article.getUserId()){
            article.setIsOwn(true);
        }
        return AppResult.success(article);
    }
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @RequestParam("id") @NonNull Long id ,
                            @RequestParam("title") @NonNull String title,
                            @RequestParam("content")@NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failure(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        Article article = articleService.selectById(id);
        if(article == null){
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        if(article.getState() == 1){
            log.warn("帖子已经截至");
            return AppResult.failure("帖子已经截止");
        }
        if(user.getId() != article.getUserId()){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        articleService.modify(id, title, content);
        return AppResult.success("编辑成功");
    }

    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(@RequestParam("id") @NonNull Long id,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failure(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        articleService.thumbsUpById(id);
        return AppResult.success("点赞成功！！");
    }

    @PostMapping("/delete")
    public AppResult deleteById(@RequestParam("id") @NonNull Long id,HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failure(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        Article article = articleService.selectById(id);
        if(article.getUserId() != user.getId()){
            log.warn(ResultCode.FAILED_USER_BANNED.getMessage());
            throw new ApplicationException(ResultCode.FAILED_USER_BANNED.getMessage());
        }
        articleService.deleteById(id);
        return AppResult.success("删除成功！！");
    }

    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(@RequestParam(value = "userId",required = false)  Long userId,HttpServletRequest request){
        if(userId == null){
            HttpSession session = request.getSession(false);
            User user = (User)session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        List<Article> articles = articleService.selectByUserId(userId);
        log.info("通过userId查询帖子列表：" + userId);
        return AppResult.success(articles);
    }


}